﻿using System;
using System.Configuration;
using System.Data.SqlClient;

namespace wall
{
    public partial class ReplyMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Admin"] == null)
                {
                    Response.Redirect("AdminLogin.aspx");
                }
            }
        }

        protected void btnReply_Click(object sender, EventArgs e)
        {
            int messageId = Convert.ToInt32(Request.QueryString["MessageId"]);
            string reply = txtReply.Text;

            string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string query = "UPDATE Messages SET AdminReply = @AdminReply WHERE Id = @Id";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@AdminReply", reply);
                command.Parameters.AddWithValue("@Id", messageId);

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    lblMessage.Text = "回复成功！";
                    txtReply.Text = "";
                    Response.Redirect("AdminPanel.aspx");
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "发生错误：" + ex.Message;
                }
            }
        }
    }
}
